﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace YArchitech.Temp
{
	public class HYDuctSpecifications : SortedSet<HYDuctSpecification>, IHYXmlSerial
	{
		public new bool Add(HYDuctSpecification item)
		{
			return item.IsValid && base.Add(item);
		}

		public void GetAllWidth(out SortedSet<int> widths)
		{
			widths = new SortedSet<int>();
			foreach (HYDuctSpecification hyductSpecification in this)
			{
				widths.Add(hyductSpecification.Width);
			}
		}

		public void GetAllHeight(out SortedSet<int> heights)
		{
			heights = new SortedSet<int>();
			foreach (HYDuctSpecification hyductSpecification in this)
			{
				heights.Add(hyductSpecification.Height);
			}
		}

		public HYDuctSpecifications GetMatchSpecificationsByFactor(double rectFactor = 4.0, double rectFactorDeviation = 1.0)
		{
			IOrderedEnumerable<HYDuctSpecification> orderedEnumerable = from sec in this
			where sec.Factor < rectFactor || Math.Abs(sec.Factor - rectFactor) < 0.5 + rectFactorDeviation
			orderby sec descending
			select sec;
			if (orderedEnumerable == null)
			{
				return null;
			}
			HYDuctSpecifications hyductSpecifications = new HYDuctSpecifications();
			hyductSpecifications.UnionWith(orderedEnumerable);
			return hyductSpecifications;
		}

		public HYDuctSpecifications GetMatchSpecificationsByWidth(Range widthRange)
		{
			HYDuctSpecifications hyductSpecifications = new HYDuctSpecifications();
			hyductSpecifications.UnionWith(from sec in this
			where widthRange.Contains((double)sec.Width)
			select sec);
			return hyductSpecifications;
		}

		public HYDuctSpecifications GetMatchSpecificationsByHeight(Range heightRange)
		{
			HYDuctSpecifications hyductSpecifications = new HYDuctSpecifications();
			hyductSpecifications.UnionWith(from sec in this
			where heightRange.Contains((double)sec.Height)
			select sec);
			return hyductSpecifications;
		}

		public HYDuctSpecification SelectSectionByVelocity(double flow, double designVelocity)
		{
			double num = double.MaxValue;
			HYDuctSpecification result = null;
			foreach (HYDuctSpecification hyductSpecification in this)
			{
				double num2 = Math.Abs(hyductSpecification.GetVelocity(flow) - designVelocity);
				if (num > num2)
				{
					num = num2;
					result = hyductSpecification;
				}
			}
			return result;
		}

		public void WriteXml(XElement xe)
		{
			foreach (HYDuctSpecification hyductSpecification in this)
			{
				XElement xelement = new XElement("Specification");
				hyductSpecification.WriteXml(xelement);
				xe.Add(xelement);
			}
		}

		public void ReadXml(XElement xe)
		{
			this.Clear();
			foreach (XElement xe2 in xe.Elements("Specification"))
			{
				HYDuctSpecification hyductSpecification = new HYDuctSpecification(HYShapeProfile.Rectangular, 800, 320);
				hyductSpecification.ReadXml(xe2);
				this.Add(hyductSpecification);
			}
		}
	}
}
